package cn.tedu.store.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.store.bean.Address;
import cn.tedu.store.bean.ResponseResult;
import cn.tedu.store.service.IAddressService;
import cn.tedu.store.service.ex.DataNotFoundException;

@Controller
@RequestMapping("/address")
public class AddressController extends BaseController {
	@Resource
	private IAddressService addressService;
	
	@RequestMapping(value="/add.do", method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> handleAdd(Address address,
			HttpSession session){
		Integer uid=getUidFromSession(session);
		address.setUid(uid);
		addressService.add(address);
		ResponseResult<Void> rr=new ResponseResult<Void>(1,"增加收货地址成功");
		return rr;
	}
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public  String showList() {
		return "address_list";
	}
	@RequestMapping(value="/get_list.do", method=RequestMethod.GET)
	@ResponseBody
	public ResponseResult<List<Address>> getAddressListByUid(HttpSession session) {
		Integer uid=getUidFromSession(session);
		ResponseResult<List<Address>> rr;
		List<Address> data= addressService.getAddressListByUid(uid);
		rr=new ResponseResult<List<Address>>(1,data);
		return rr;
	}
	@RequestMapping(value="/delete.do", method=RequestMethod.GET)
	@ResponseBody
	public ResponseResult<Void> handleDelete(Integer id,HttpSession session){
		ResponseResult<Void> rr;
		Integer uid=getUidFromSession(session);
		try {
			addressService.delete(id, uid);
			rr=new ResponseResult<Void>(1, "删除成功");
		}catch(DataNotFoundException e){
			rr=new ResponseResult<Void>(0, e.getMessage());
		}
		return rr;
	}
	@RequestMapping(value="/handle_update.do", method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> handleUpdate(Address address,HttpSession session){
		ResponseResult<Void> rr;
		Integer uid=getUidFromSession(session);
		address.setUid(uid);
		Integer affectRows=addressService.update(address);
		if(affectRows==1) {
			rr=new ResponseResult<Void>(1, "修改成功");
		}else {
			rr=new ResponseResult<Void>(0, "修改失败");
		}
		return rr;
	}
	@RequestMapping(value="/get.do", method=RequestMethod.GET)
	@ResponseBody
	public ResponseResult<Address> getAddressByIdAndUid(Integer id,HttpSession session){
		ResponseResult<Address> rr;
		Integer uid=getUidFromSession(session);
		Address address=addressService.getAddressByIdAndUid(id, uid);
		rr=new ResponseResult<Address>(1,address);
		return rr;
	}
	@RequestMapping(value="/setDefault.do", method=RequestMethod.GET)
	@ResponseBody
	public ResponseResult<Void> handleSetDefault(Integer id,HttpSession session){
		ResponseResult<Void> rr;
		Integer uid=getUidFromSession(session);
		 
		try{
			addressService.setDefault(uid, id);
			rr=new ResponseResult<Void>(1, "OK");
		}catch(DataNotFoundException e) {
			rr=new ResponseResult<Void>(0, e.getMessage());
		}
		return rr;
	}
}
